package com.test.service.impl;

import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResCode;
import com.test.controller.Admininfo.ResData;
import com.test.entity.TaskInfo;
import com.test.dao.TaskInfoDao;
import com.test.service.TaskInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务(TaskInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 11:01:48
 */
@Service("taskInfoService")
public class TaskInfoServiceImpl implements TaskInfoService {
    @Resource
    private TaskInfoDao taskInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TaskInfo queryById(Integer id) {
        return this.taskInfoDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param taskInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public ResData queryByPage(TaskInfo taskInfo, PageRequest pageRequest) {
        int total =Integer.parseInt( String.valueOf(this.taskInfoDao.count(taskInfo)));
        int pageSize = pageRequest.getPageSize();
        int page;
        page = total%pageSize==0? total/pageSize:total/pageSize+1;
        List<TaskInfo> taskInfoList = taskInfoDao.queryAllByLimit(taskInfo, pageRequest);
        List<MyObject> objectList = taskInfoList.stream().map(taskInfo1 -> new MyObject(taskInfo1)).collect(Collectors.toList());
        return new ResData(ResCode.SUCCESS,objectList,total,page);
    }

    /**
     * 新增数据
     *
     * @param taskInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(TaskInfo taskInfo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String format1 = format.format(new Date());
        taskInfo.setCreateTime(format1);
        return this.taskInfoDao.insert(taskInfo);
    }

    /**
     * 修改数据
     *
     * @param taskInfo 实例对象
     * @return 实例对象
     */
    @Override
    public TaskInfo update(TaskInfo taskInfo) {
        this.taskInfoDao.update(taskInfo);
        return this.queryById(taskInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.taskInfoDao.deleteById(id) > 0;
    }
}
